﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KZ.Utilities;
using KZ.Express.H.ENT;
using KZ.Express.H.Presenter;
using KZ.Express.H.BLL;

namespace KZ.Express.H.Settings.Users
{
    public partial class UserDetailView : BaseView
    {
        #region Private Variables


        #endregion Private Variables

        #region Private Methods

        private void UserDetailView_Load(object sender, EventArgs e)
        {
            LoadDataRole();
            LoadData();
        }

        private void LoadDataRole()
        {
            cbRole.ValueMember = "RoleID";
            cbRole.DisplayMember = "RoleName";
            cbRole.DataSource = _presenter.LoadDataTable<RoleENT>(null);
        }

        private void LoadData()
        {
            try
            {
                switch (this.ActionName)
                {
                    case "Insert":
                        break;

                    case "Update":
                        _presenter.LoadEntity<UserENT>();
                        break;
                }
            }
            catch (BusinessLogicException ex)
            {
                ShowMessageBoxError(ex.Message);
            }
        }


        #endregion Private Methods

        #region Constructors

        public UserDetailView()
        {
            InitializeComponent();

            _presenter = new BasePresenter(this);
            this.ObjectID = (int)Objects.User;
        }

        #endregion Constructors

        #region Public Properties

        [Search]
        public int UserID
        {
            get { return base.EntityID; }
        }

        public override BaseENT Entity
        {
            get
            {
                UserENT user = new UserENT();
                user.UserID = base.EntityID;
                user.FullName = txtFullName.Text;
                user.Username = txtUserName.Text;
                user.Password = txtPassword.Text;
                user.RoleID = (int)cbRole.SelectedValue;

                user.CreatedBy = UserENT._USERID;
                user.UpdatedBy = UserENT._USERID;
                user.ValidateData();

                return user;
            }
            set
            {
                UserENT user = value == null ? new UserENT() : value as UserENT;
                txtFullName.Text = user.FullName;
                txtUserName.Text = user.Username;
                txtPassword.Text = user.Password;

                if (!String.IsNullOrEmpty(Utility.StringParse(user.RoleID)))
                    cbRole.SelectedValue = Utility.StringParse(user.RoleID);
            }
        }

        #endregion Public Properties

        #region Public Methods

        public override void Save()
        {
            try
            {
                _presenter.Save();
            }
            catch (BusinessLogicException ex)
            {
                ShowMessageBoxError(ex.Message);
            }
        }

        #endregion Public Methods
    }
}
